package com.tedu.comment.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tedu.comment.BDrespones.DataItem;
import com.tedu.comment.BDrespones.Hit;
import com.tedu.comment.BDrespones.Result;
import com.tedu.comment.BDrespones.WordHitPosition;
import com.tedu.comment.mapper.AuditCommentMapper;
import com.tedu.comment.mapper.CommentMapper;
import com.tedu.comment.pojo.dto.CommentQuery;
import com.tedu.comment.pojo.entity.AuditComment;
import com.tedu.comment.pojo.entity.Comment;
import com.tedu.comment.pojo.vo.AuditCommentVO;
import com.tedu.comment.pojo.vo.CommentVO;
import com.tedu.comment.service.CommentService;
import com.tedu.comment.tools.TextCensor;
import com.tedu.common.response.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl  implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    AuditCommentMapper auditCommentMapper;
    @Override
    public String audit(CommentQuery commentQuery) {
        log.debug("审核评论:{}", commentQuery);
        commentQuery.setContent(commentQuery.getContent().trim());
        if (commentQuery.getContent().length() == 0) {
            //存入评论表
            return "评论成功";
        }
        TextCensor textCensor = new TextCensor();
       String  result = textCensor.TextCensor(commentQuery.getContent());
        System.out.println("*************" + result);
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            Result result1 = objectMapper.readValue(result, Result.class);
//            DataItem dataItem = result1.getData().get(0);
            String conclusion = result1.getConclusion();
            if (conclusion.equals("合规")){
                Comment comment = new Comment();
                log.debug("评论内容:{}", comment);
                BeanUtils.copyProperties(commentQuery, comment);
                comment.setStatus(1);
                comment.setCreateAt(new Date());
                commentMapper.insert(comment);
                return "评论成功";
            }else {
                DataItem dataItem = result1.getData().get(0);
                AuditComment auditComment = new AuditComment();
                BeanUtils.copyProperties(commentQuery, auditComment);
                Hit hit = dataItem.getHits().get(0);
                for (int i = 0; i < hit.getWordHitPositions().size(); i++) {
                    WordHitPosition wordHitPosition = hit.getWordHitPositions().get(i);
                    String keyword = wordHitPosition.getKeyword();
                    List<List<Integer>> positions = wordHitPosition.getPositions();
                    List<Integer> list = positions.get(0);
                    Integer start = list.get(0);
                    Integer end = list.get(1);
                    System.out.println(end - start + "---------------------------------" + start + "****" + end);
                    StringBuilder starB = new StringBuilder();
                    for (int j = start; j < end + 1; j++) {
                        starB.append("*");
                    }
                    String star = starB.toString();
                    String newContent = commentQuery.getContent().replace(keyword, star);
                    commentQuery.setContent(newContent);
                    System.out.println(newContent);
                }
                System.out.println(commentQuery.getContent());
                 Comment comment = new Comment();
                BeanUtils.copyProperties(commentQuery, comment);
                comment.setContent(commentQuery.getContent());
                comment.setStatus(3);
                comment.setCreateAt(new Date());
                commentMapper.insert(comment);
                System.out.println(commentQuery.getContent()+"59959595599");
                auditComment.setAuditContent(commentQuery.getContent());
                auditComment.setCommentId(comment.getCommentId());
                auditComment.setStatus(3);
                auditComment.setCreateAt(new Date());
                auditCommentMapper.insert(auditComment);


            }

//            WordHitPosition wordHitPosition = hit.getWordHitPositions().get(0);
//            String keyword = wordHitPosition.getKeyword();
//            System.out.println("关键字是: " + keyword);

        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }


        // 查找 "conclusion":" 的位置
//            int startIndex = result.indexOf("\"conclusion\":\"") + "\"conclusion\":\"".length();
//            // 结束位置是紧接着的下一个双引号
//            int endIndex = result.indexOf("\"", startIndex);
//            System.out.println("合规的起始下标是: " + startIndex);
//            System.out.println("合规的结束下标是: " + endIndex);
//            // 验证提取的内容
//            String conclusion = result.substring(startIndex, endIndex);
//            System.out.println("提取的内容是: " + conclusion);  // 应该输出: 合规
//            if (conclusion.equals("合规")) {
//                return "评论成功";
//            } else {
//                Comment comment = new Comment();
//                BeanUtils.copyProperties(commentQuery, comment);
//                comment.setAuditResult("AI审核不合规");
//                comment.setAuditStatus(2);
//                comment.setDetailedReason(result);
//                commentMapper.insert(comment);
//                return "评论失败";
//            }
        return null;

    }


    @Override
    public PageData loadComment(CommentQuery commentQuery) {
        log.debug("用户评论业务层参数-入参:{}",commentQuery);
        if (commentQuery.getStatus()!=1) {
            List<AuditCommentVO> auditCommentVOList = auditCommentMapper.loadComment(commentQuery);
            PageData interceptCommentList = new PageData();
            interceptCommentList.setList(auditCommentVOList);
            interceptCommentList.setTotal(auditCommentMapper.loadCommentCount(commentQuery));
            return interceptCommentList;
        }else{
            List<CommentVO> commentVOList = commentMapper.loadComment(commentQuery);
            PageData commentList = new PageData();
            commentList.setList(commentVOList);
            commentList.setTotal(commentMapper.loadCommentCount(commentQuery));
            return commentList;
        }
    }

    @Override
    public void pass(CommentQuery commentQuery) {
        // 这里要调用客户端的审核结果进行保存
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentQuery,comment);
        comment.setUpdateAt(new Date());
        commentMapper.update(comment);
        //*****************************
        AuditComment auditComment = new AuditComment();
        auditComment.setCommentId(commentQuery.getCommentId());
        auditComment.setAuditContent(commentQuery.getContent());
        auditComment.setStatus(commentQuery.getStatus());
        auditComment.setAuditContentBak(commentQuery.getAuditContent());
        auditComment.setUpdateAt(new Date());
        auditCommentMapper.update(auditComment);


    }

    @Override
    public void confirm(CommentQuery commentQuery) {
        log.debug("用户评论业务层参数-入参:{}",commentQuery);
        Comment comment = new Comment();
         BeanUtils.copyProperties(commentQuery,comment);
        comment.setUpdateAt(new Date());
        log.debug("用户评论业务层参数-入参:{}",comment);
        commentMapper.update(comment);
        AuditComment auditComment = new AuditComment();
        auditComment.setCommentId(commentQuery.getCommentId());
        auditComment.setStatus(commentQuery.getStatus());
        auditComment.setUpdateAt(new Date());
        log.debug("用户评论业务层参数AI-入参:{}",auditComment);
        auditCommentMapper.update(auditComment);


    }

    @Override
    public void back(CommentQuery commentQuery) {
        log.debug("用户评论审核撤回业务层参数-入参:{}",commentQuery);
        Comment comment = new Comment();
        comment.setCommentId(commentQuery.getCommentId());
        comment.setStatus(commentQuery.getStatus());
        comment.setUpdateAt(new Date());
        comment.setContent(commentQuery.getAuditContentBak());
        commentMapper.update(comment);
        AuditComment auditComment = new AuditComment();
        auditComment.setCommentId(commentQuery.getCommentId());
        auditComment.setStatus(commentQuery.getStatus());
        auditComment.setUpdateAt(new Date());
        auditComment.setAuditContent(commentQuery.getAuditContentBak());
        auditCommentMapper.update(auditComment);
    }
}
